<?php

namespace app\common\model\mycurrency;

use app\admin\model\Admin;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class Merchant extends Model
{

    //use SoftDelete;

    

    // 表名
    protected $name = 'mycurrency_merchant';

    protected static $group_id = 5;//顶级角色组id

    //生成商铺唯一编号
    public static function getNumger(){
        $number = date('YmdHis') . mt_rand(1000, 9999);
        $exists = self::where('number', $number)->value('id');
        if (!$exists) {
            return $number;
        }
        return self::getNumger();
    }


    /**
     * 添加后台管理员账号
     * @param array $admin 管理员信息
    */
    public static function adminAdd($admin){
        if(Admin::where(['username' => $admin['username']])->find()){
            throw new Exception('管理员账号重复');
            return false;
        }
        if(Admin::where(['mobile' => $admin['mobile']])->find()){
            throw new Exception('手机号重复');
            return false;
        }
        if(Admin::where(['nickname' => $admin['nickname']])->find()){
            throw new Exception('商铺名称重复');
            return false;
        }
        $group_rules = AuthGroup::where(['id' => self::$group_id])->value('rules');
        $group_result = AuthGroup::create([
            'pid' => self::$group_id,
            'name' => $admin['nickname'],
            'rules' => $group_rules,
            'createtime' => time(),
            'status' => 'normal'
        ]);
        $admin_result = Admin::create($admin);
        AuthGroupAccess::create([
            'uid' => $admin_result->id,
            'group_id' => $group_result->id,
        ]);
    }




}
